<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;
use DB;

class Linkslists extends Model
{
    protected $table = 'links_lists';
    protected $primaryKey = 'id';

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public static function getLinks($links_id)
    {
        return self::where("links_id",$links_id)->first();
    }

    public static function getApiLinks($links_id)
    {
        return self::select("id","title","platfrom","platfrom_type","describe","type","msg","status","links_id","limit_pv","end_time")->where("links_id",$links_id)->first();
    }

    public static function getLinksListsUsersId($links_id)
    {
        return self::select("users_id")->where("links_id",$links_id)->first();
    }

    public static function jumpGetApiLinks($links_id)
    {
        return self::select("links_id","platfrom_type","msg","limit_status","limit_pv","left_pv","end_time","type","wechat","qrcode_switch","qrcode_upper","qrcode_daily","disable")->where("links_id",$links_id)->first();
    }

    public static function getLinksSimple($links_id)
    {
        return self::select("links_id","title","describe","platfrom_type","msg","status","end_time","limit_pv","left_pv","users_id","type")->where("links_id",$links_id)->first();
    }

    public static function apiLinksSimple($links_id)
    {
        return self::select("links_id","users_id","title","describe","platfrom_type","msg","status","end_time","limit_pv","left_pv")->where("links_id",$links_id)->first();
    }

    public static function jsLinksSimple($links_id)
    {
        return self::select("platfrom_type","miniapp_type","appid","path")->where("links_id",$links_id)->first();
    }

    public static function linksListMsg($links_id)
    {
        return self::select("msg")->where("links_id",$links_id)->first();
    }

    public static function getLinkMiniappStatus($links_id)
    {
        return self::select("miniapp_status","platfrom_type","miniapp_type","appid","path","end_time","limit_pv")->where("links_id",$links_id)->first();
    }

    public static function createLinkslists($users_id,$title,$msg,$limit_pv,$limit_status,$end_time,$links_id,$platfrom,$platfrom_type,$appid,$original_id,$path,$miniapp_type,$qrcode_switch,$qrcode_upper,$qrcode_daily,$wechat,$miniapp_status)
    {
        return self::insertGetId(["users_id"=>$users_id,"title"=>$title,"msg"=>$msg,"limit_pv"=>$limit_pv,"left_pv"=>$limit_pv,"end_time"=>$end_time,"limit_status"=>$limit_status,"links_id"=>$links_id,"platfrom"=>$platfrom,"platfrom_type"=>$platfrom_type,"appid"=>$appid,"original_id"=>$original_id,"path"=>$path,"miniapp_type"=>$miniapp_type,"qrcode_switch"=>$qrcode_switch,"qrcode_upper"=>$qrcode_upper,"qrcode_daily"=>$qrcode_daily,"wechat"=>$wechat,"miniapp_status"=>$miniapp_status,"created_at"=>time()]);
    }

    public static function updateLinkslists($links_id,$title,$msg,$platfrom,$platfrom_type,$appid,$original_id,$path,$miniapp_type,$qrcode_switch,$qrcode_upper,$qrcode_daily,$wechat,$miniapp_status)
    {
        return self::where("links_id",$links_id)->update(["title"=>$title,"msg"=>$msg,"platfrom"=>$platfrom,"platfrom_type"=>$platfrom_type,"appid"=>$appid,"original_id"=>$original_id,"path"=>$path,"miniapp_type"=>$miniapp_type,"qrcode_switch"=>$qrcode_switch,"qrcode_upper"=>$qrcode_upper,"qrcode_daily"=>$qrcode_daily,"wechat"=>$wechat,"miniapp_status"=>$miniapp_status]);
    }

    public static function updateLinksInfo($id,$data)
    {
        return self::where("id",$id)->update($data);
    }

    public static function getUsersLinks($users_id)
    {
        return self::select("links_id","title","platfrom","platfrom_type","status","limit_pv","limit_status","left_pv","end_time","created_at")->where("users_id",$users_id)->where("disable",1)->limit(30)->get();
    }

    public static function getUsersLinksSample($users_id)
    {
        return self::select("links_id","created_at")->where("users_id",$users_id)->where("disable",1)->get();
    }

    public static function adminUsersLinksSample($users_id)
    {
        return self::select("links_id","title","remark")->where("users_id",$users_id)->where("disable",1)->take(200)->get();
    }

    public static function getLinkSample($links_id)
    {
        return self::select("links_id","created_at")->where("links_id",$links_id)->first();
    }

    public static function getdemoLinks()
    {
        return self::select("links_id","title","platfrom_type","platfrom")->where("users_id",7)->where("title","like","%演示示例%")->where("disable",1)->limit(30)->get();
    }

    public static function adminGetUsersLinks($users_id,$title='',$limit=20)
    {
        $query = self::select("links_lists.*","links_lists_minapps.name","links_lists_minapps.status")->leftJoin('links_lists_minapps', 'links_lists.appid', '=', 'links_lists_minapps.appid')->where("links_lists.users_id",$users_id)->where("links_lists.disable",1);
        if (!empty($title)) $query->where("links_lists.title","like","%".$title."%");
        return $query->orderBy('links_lists.id', 'desc')->paginate($limit);
    }

    public static function getLinksDetial($id)
    {
        return self::where("id",$id)->first();
    }

    public static function linksListsDisable($users_id,$links_id)
    {
        return self::where("users_id",$users_id)->where("links_id",$links_id)->update(["disable"=>2]);
    }

    public static function linksListsupdatePath($id,$original_id,$path)
    {
        return self::where("id",$id)->update(["original_id"=>$original_id,"path"=>$path]);
    }

    public static function incrementInfo($id, $key, $value)
    {
        return self::where('id', $id)->increment($key, $value);
    }

    public static function decrementInfo($id, $key)
    {
        return self::where('id', $id)->decrement($key, 1);
    }

    public static function usersLinksListsCount($users_id)
    {
        return self::where('users_id', $users_id)->where("disable",1)->count();
    }

    public static function usersAllLinksListsCount($users_id)
    {
        return self::where('users_id', $users_id)->count();
    }

    public static function getLinksUrllink($links_id)
    {
        return self::select("links_lists.appid","links_lists.path","links_lists.miniapp_type","links_lists.left_pv","links_lists.end_time","links_lists.disable","links_lists.platfrom_type","links_lists_minapps.appid","links_lists_minapps.secret")
                    ->leftJoin('links_lists_minapps', 'links_lists.appid', '=', 'links_lists_minapps.appid')
                    ->where("links_lists.links_id",$links_id)
                    ->first();
    }

    public static function adminLinksLists($users_id,$title,$links_id)
    {
        $query = self::where("users_id",$users_id)->where("disable",1);
        if (!empty($title)) $query->where("title","like","%".$title."%");
        if (!empty($links_id)) $query->where("links_id",$links_id);
        return $query->orderBy("id","DESC")->paginate(25);
    }

    public static function manageLinkLists($users_id,$title,$links_id)
    {
        $query = self::query();
        if (!empty($title)) $query->where("title","like","%".$title."%");
        if (!empty($links_id)) $query->where("links_id",$links_id);
        if (!empty($users_id)) $query->where("users_id",$users_id);
        return $query->orderBy("id","DESC")->paginate(25);
    }

    public static function adminMemberLinksLists($title,$links_id,$platfrom_type,$end_time,$appid)
    {
        $query = self::select("links_lists.*","users.nike","users.avatar","users.username","users.wechat")->leftJoin('users', 'links_lists.users_id', '=', 'users.id')->where("links_lists.limit_pv",">",1000)->where("links_lists.disable",1);
        if (!empty($title)) $query->where("links_lists.title","like","%".$title."%");
        if (!empty($links_id)) $query->where("links_lists.links_id",$links_id);
        if (!empty($platfrom_type)) $query->where("links_lists.platfrom_type",$platfrom_type);
        if (!empty($end_time)) $query->where("links_lists.end_time",">=",$end_time);
        if (!empty($appid)) $query->where("links_lists.appid",$appid);
        return $query->orderBy("links_lists.users_id","DESC")->paginate(25);
    }

    public static function updateLinksListsStatus($users_id,$limit_status,$limit_pv,$end_time)
    {
        return self::where("users_id",$users_id)->where("disable",1)->update(["limit_status"=>$limit_status,"limit_pv"=>$limit_pv,"left_pv"=>$limit_pv,"end_time"=>$end_time]);
    }

    public static function getUsersLinksListsCount($users_ids)
    {
        return self::select(DB::raw('count(id) as links_count, users_id'))->whereIn("users_id",$users_ids)->where("disable",1)->groupBy('users_id')->get();
    }

    public static function updatePlatfromType($links_id,$platfrom_type)
    {
        return self::where("links_id",$links_id)->update(["platfrom_type"=>$platfrom_type]);
    }

    public static function updateLinksLeftPv($links_id,$page_view)
    {
        return self::where("links_id",$links_id)->decrement("left_pv", $page_view);
    }

    public static function updateLinksMiniappType($appid,$miniapp_type)
    {
        return self::where("appid",$appid)->update(["miniapp_type"=>$miniapp_type]);
    }

    public static function updateUsersMiniappstatus($users_id,$miniapp_status)
    {
        return self::where("users_id",$users_id)->update(["miniapp_status"=>$miniapp_status]);
    }

    public static function updateLinksAppid($oldappid,$appid,$original_id,$miniapp_type)
    {
        return self::where("appid",$oldappid)->update(["appid"=>$appid,"original_id"=>$original_id,"miniapp_type"=>$miniapp_type]);
    }

    public static function updateUsersLinksAppid($users_id,$oldappid,$appid,$original_id,$miniapp_type)
    {
        return self::where("users_id",$users_id)->where("appid",$oldappid)->update(["appid"=>$appid,"original_id"=>$original_id,"miniapp_type"=>$miniapp_type]);
    }
    //更新开通会员后依旧使用免费小程序的链接
    public static function updateFreeMemberLinksAppid($oldappid,$appid,$original_id,$miniapp_type)
    {
        return self::where("appid",$oldappid)->where('limit_pv','>',1000)->where('status',1)->where('disable',1)->where('end_time','>',time())->update(["appid"=>$appid,"original_id"=>$original_id,"miniapp_type"=>$miniapp_type]);
    }

    public static function getLinksIdByAppid($appid)
    {
        return self::select("links_id")->where("appid",$appid)->where("disable",1)->first();
    }

    public static function updateLinksListsRemark($links_id,$remark)
    {
        return self::where("links_id",$links_id)->update(["remark"=>$remark]);
    }

    public static function updateLinksListsAppid($links_id,$appid)
    {
        return self::where("links_id",$links_id)->update(["appid"=>$appid]);
    }

    public static function updateUsersLinksListsDisable($users_id,$disable)
    {
        return self::where("users_id",$users_id)->update(["disable"=>$disable]);
    }
}
